/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.dto.ProductDockedDto;
import com.yami.shop.bean.model.ProductDocked;
import com.yami.shop.dao.ProductDockedMapper;
import com.yami.shop.service.ProductDockedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 已对接商品Service实现类
 */
@Service
public class ProductDockedServiceImpl extends ServiceImpl<ProductDockedMapper, ProductDocked> implements ProductDockedService {

    @Autowired
    private ProductDockedMapper productDockedMapper;

    @Override
    public IPage<ProductDockedDto> getProductDockedDtoPage(Page page, Long shopId) {
        return productDockedMapper.getProductDockedDtoPage(page, shopId);
    }
    
    @Override
    public IPage<ProductDockedDto> searchProductDockedDtoPage(Page page, ProductDockedDto productDockedDto) {
        return productDockedMapper.searchProductDockedDtoPage(page, productDockedDto);
    }

    @Override
    public ProductDockedDto getProductDockedDtoById(Long id) {
        return productDockedMapper.getProductDockedDtoById(id);
    }

    @Override
    public ProductDocked getByProdIdAndShopId(Long prodId, Long shopId) {
        return productDockedMapper.getByProdIdAndShopId(prodId, shopId);
    }

    @Override
    public List<ProductDocked> getBySupShopId(Long supShopId) {
        return productDockedMapper.getBySupShopId(supShopId);
    }

    @Override
    public IPage<ProductDockedDto> getProductDockedDtoPageByCategoryId(Page page, Long shopCategoryId, Long shopId) {
        return productDockedMapper.getProductDockedDtoPageByCategoryId(page, shopCategoryId, shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addProductDocked(ProductDocked productDocked) {
        // 检查是否已存在相同的对接商品
        ProductDocked existProductDocked = getByProdIdAndShopId(productDocked.getProdId(), productDocked.getShopId());
        if (existProductDocked != null) {
            return false;
        }
        
        // 设置默认值
        Date now = new Date();
        productDocked.setCreateTime(now);
        productDocked.setUpdateTime(now);
        if (productDocked.getStatus() == null) {
            productDocked.setStatus(0); // 默认下架
        }
        
        return this.save(productDocked);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProductDocked(ProductDocked productDocked) {
        // 检查是否存在
        ProductDocked dbProductDocked = this.getById(productDocked.getId());
        if (dbProductDocked == null) {
            return false;
        }
        
        // 设置更新时间
        productDocked.setUpdateTime(new Date());
        
        return this.updateById(productDocked);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, Integer status) {
        return productDockedMapper.updateStatus(id, status) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteProductDocked(Long id) {
        return this.removeById(id);
    }
} 